package cuit.epoch.pymjl.security.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cuit.epoch.pymjl.security.common.result.Result;
import cuit.epoch.pymjl.security.entity.TCollection;
import cuit.epoch.pymjl.security.entity.TShop;
import cuit.epoch.pymjl.security.mapper.TCollectionMapper;
import cuit.epoch.pymjl.security.mapper.TShopMapper;
import cuit.epoch.pymjl.security.service.TCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

@RestController
public class TCollectionController {

    private final TCollectionService tCollectionService;
    private final TCollectionMapper tCollectionMapper;
    private final TShopMapper tShopMapper;
    @Autowired
    public TCollectionController(TCollectionService tCollectionService,TCollectionMapper tCollectionMapper,TShopMapper tShopMapper) {
        this.tCollectionService=tCollectionService;
        this.tCollectionMapper=tCollectionMapper;
        this.tShopMapper=tShopMapper;
    }


    //添加收藏
    @RequestMapping("/addCollection")
    public Result addCollection(@RequestBody String jsonParam){

        //json字符串转json对象
        JSONObject jsonObj = JSON.parseObject(jsonParam);
        //将带数据的json字符串从data中取出
        String s = jsonObj.get("data").toString();
        //将json字符串转为Java对象
        TCollection tCollection = JSON.parseObject(s , TCollection.class);
        //存入数据库
        boolean save = tCollectionService.save(tCollection);
        //判定是否成功
        return save? Result.suc("收藏成功") : Result.fall("收藏失败！");
    }

    //根据id返回用户的收藏
    @RequestMapping("/getCollection")
    public Result getCollection(@RequestBody String jsonParam){
        //json字符串转json对象
        JSONObject jsonObj = JSON.parseObject(jsonParam);
        //将带数据的json字符串从data中取出
        String s = jsonObj.get("data").toString();
        //将json字符串转为Java对象
        TCollection tCollection = JSON.parseObject(s , TCollection.class);
        //设置查询条件
        LambdaQueryWrapper<TCollection> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(TCollection::getUserId,tCollection.getUserId());
        List<TCollection> tCollections = tCollectionMapper.selectList(lambdaQueryWrapper);
        //将记录中的商品id取出
        List<Integer> list = new ArrayList<>();
        for(TCollection tCollection1:tCollections){
            list.add(tCollection1.getShopId());
        }
        //根据收藏的商品id查询商品的详细信息
        LambdaQueryWrapper<TShop> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(TShop::getId,list);
        List<TShop> tShops = tShopMapper.selectList(lambdaQueryWrapper1);

        //java对象转json字符串
        String jsonStr= JSON.toJSONString(tShops);
        //json字符串转json对象
        JSONArray jsonArray = JSON.parseArray(jsonStr);
        //返回数据
        return Result.suc(jsonArray);
    }
    //删除收藏
    @RequestMapping("/deleteCollection")
    public Result deleteCollection(@RequestBody String jsonParam){
        //json字符串转json对象
        JSONObject jsonObj = JSON.parseObject(jsonParam);
        //将带数据的json字符串从data中取出
        String s = jsonObj.get("data").toString();
        //将json字符串转为Java对象
        TCollection tCollection = JSON.parseObject(s , TCollection.class);
        //根据用户id和商品id删除收藏
        //封装条件
        LambdaQueryWrapper<TCollection> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(TCollection::getUserId,tCollection.getUserId());
        lambdaQueryWrapper.eq(TCollection::getShopId,tCollection.getShopId());
        boolean remove = tCollectionService.remove(lambdaQueryWrapper);

        //判定是否成功
        return remove? Result.suc("删除收藏成功") : Result.fall("删除收藏失败！");
    }
}
